package com.china08.yxyapi.api.manage.stu.mystudy.huixue.exercise;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlan;
import com.china08.yxyapi.entity.mg.huixue.YxyHuixuePlanKnow;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanKnowTrainRepository;
import com.china08.yxyapi.repository.mg.huixue.YxyHuixuePlanRepository;

/**
 * 获取当前练习的知识点详细信息
 *
 */
@RestController
public class YxyHuixuePlanKnowByOneController {

    @Autowired
    private YxyHuixuePlanKnowRepository yxyHuixuePlanKnowRepository;

    @Autowired
    private YxyHuixuePlanKnowTrainRepository yxyHuixuePlanKnowTrainRepository;

    @Autowired
    private YxyHuixuePlanRepository yxyHuixuePlanRepository;

    @RequestMapping(value = "/api_yw/manage/stu/mystudy/huixue/exercise/planknow/sch", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {

        YxyHuixuePlanKnow planKnow = yxyHuixuePlanKnowRepository.findTopByPkId(reqModel.id);

        YxyHuixuePlan plan = yxyHuixuePlanRepository.findOne(planKnow.getPlanId());

        RespModel model = new RespModel();
        if (planKnow != null) {
            model.id = planKnow.getPkId();
            model.planId = planKnow.getPlanId();
            model.knowName = planKnow.getKnowName();
            model.attention = planKnow.getAttention();
            model.progress = planKnow.getProgress();

            model.countTrain = yxyHuixuePlanKnowTrainRepository.countByStudentIdAndPlanIdAndKnowId(plan.getStudentId(),
                    planKnow.getPlanId(), planKnow.getKnowId());
        }
        return model;
    }

    /**
     * 请求参数
     * 
     */
    protected static class ReqModel extends YxyApiReqModel {

        public String id;

        public void check() {
            Assert.notNull(StringUtils.defaultIfBlank(id, null), "学习计划知识点id不能为空");
        }
    }

    /**
     * 响应参数
     * 
     */
    protected static class RespModel extends YxyApiRespModel {

        public String id;

        // 计划id
        public String planId;

        // 知识点名称
        public String knowName;

        // 关注度
        public Integer attention;

        // 完成度
        public Double progress;

        // 练习次数
        public Long countTrain;
    }
}
